package flc.ast.dialog;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.PrivacyVideoActivity;
import flc.ast.databinding.DialogSetPasswordBinding;
import flc.ast.utils.Content;
import flc.ast.utils.GestureLockLayout;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.SPUtil;

public class SetPasswordDialog extends BaseSmartDialog<DialogSetPasswordBinding> implements View.OnClickListener {

    public boolean isSetPassword = false;


    public SetPasswordDialog(@NonNull Context context) {
        super(context);
    }


    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_set_password;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvPasswordForget.setOnClickListener(this);

        mDataBinding.tvPasswordForget.setVisibility(isSetPassword ? View.INVISIBLE : View.VISIBLE);
        //设置手势解锁view 每行每列点的个数
        mDataBinding.gestureView.setDotCount(3);
        //设置手势解锁view 最少连接数
        mDataBinding.gestureView.setMinCount(3);
        //默认解锁样式为手Q手势解锁样式
//             mDataBinding.gestureView.setLockView();
        if (isSetPassword) {
            //设置手势解锁view 模式为重置密码模式
            mDataBinding.gestureView.setMode(GestureLockLayout.RESET_MODE);
        } else {
            mDataBinding.gestureView.setMode(GestureLockLayout.VERIFY_MODE);
            String spPassword = SPUtil.getString(getContext(), Content.myPassword, "");
            mDataBinding.gestureView.setAnswer(spPassword);
        }
        mDataBinding.gestureView.setOnLockResetListener(new GestureLockLayout.OnLockResetListener() {
            @Override
            public void onConnectCountUnmatched(int connectCount, int minCount) {
                //连接数小于最小连接数时调用
                ToastUtils.showShort(getContext().getString(R.string.min_connect, minCount));
                resetGesture();
            }

            @Override
            public void onFirstPasswordFinished(List<Integer> answerList) {
                //重置
                resetGesture();
                mDataBinding.tvPasswordTitle.setText(getContext().getString(R.string.verify_password));
            }

            @Override
            public void onSetPasswordFinished(boolean isMatched, List<Integer> answerList) {
                //第二次密码绘制成功时调用
                if (isMatched) {
                    //两次答案一致，保存
                    //do something
                    String password = answerList.toString();
                    listener.click(password);
                    dismiss();
                } else {
                    ToastUtils.showShort(getContext().getString(R.string.the_passwords_entered_twice_are_inconsistent));
                    resetGesture();
                }
            }
        });
        mDataBinding.gestureView.setOnLockVerifyListener(new GestureLockLayout.OnLockVerifyListener() {
            @Override
            public void onGestureSelected(int id) {

            }

            @Override
            public void onGestureFinished(boolean isMatched) {
                //绘制手势解锁完成时调用

                if (isMatched) {
                    //密码匹配
                    getContext().startActivity(new Intent(getContext(), PrivacyVideoActivity.class));
                    dismiss();
                } else {
                    //不匹配
                    mDataBinding.tvPasswordTitle.setText(getContext().getString(R.string.password_error));
                    mDataBinding.tvPasswordTitle.setTextColor(Color.parseColor("#FF4545"));
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mDataBinding.tvPasswordTitle.setText(getContext().getString(R.string.draw_password));
                            mDataBinding.tvPasswordTitle.setTextColor(Color.parseColor("#FFFFFF"));
                        }
                    }, 500);
                    resetGesture();
                }
            }

            @Override
            public void onGestureTryTimesBoundary() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvPasswordForget://忘记密码
                dismiss();
                listener.forgetPassword();
                break;
        }
    }

    /**
     * 重置
     */
    private void resetGesture() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mDataBinding.gestureView.resetGesture();
            }
        }, 500);
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {

        void click(String password);

        void forgetPassword();
    }
}
